package com.qlylkj.lbk.model.repository

import com.qlylkj.lbk.model.api.BaseRepository
import com.qlylkj.lbk.model.api.CompanyApi
import com.qlylkj.lbk.model.api.ResultResponse
import com.qlylkj.lbk.model.api.RetrofitHelper
import com.qlylkj.lbk.model.bean.CoChainRecordList
import com.qlylkj.lbk.model.bean.Company
import com.qlylkj.lbk.model.bean.CompanyList
import okhttp3.RequestBody

class CompanyRepository : BaseRepository() {

    private val companyApi by lazy { RetrofitHelper.getService(CompanyApi::class.java) }

    private fun requestBodyGetCompanyList(
        companyName: String,
        bindingStatus: Int,
        offset: Int,
        pageSize: Int
    ): MutableMap<String, Any> {
        val map = mutableMapOf<String, Any>()
        map["companyName"] = "*$companyName*"
        if(bindingStatus != 0){
            map["bindingStatus"] = bindingStatus
        }
        map["pageSize"] = pageSize
        map["pageNo"] = offset
        return map
    }

    private fun requestBodyGetCompany(companyId: String): MutableMap<String, Any> {
        val map = mutableMapOf<String, Any>()
        map["id"] = companyId
        return map
    }

    private fun requestBodyCompanyFromCoChain(companyId: String): RequestBody {
        val map = mutableMapOf<String, Any>()
        map["ids"] = companyId
        map["checkStatus"] = 4
        return requestJsonBody(map)
    }

    suspend fun getCompanyList(
        companyName: String,
        bindingStatus: Int = 0,
        offset: Int = 1,
        pageSize: Int = 10
    ): ResultResponse<CompanyList> {
        return safeApiCallEntity(
            call = { requestGetCompanyList(companyName, bindingStatus, offset, pageSize) },
            errorMessage = "获取搜索公司信息列表数据错误"
        )
    }

    private fun requestBodyGetUserCoChainRecord(userId: String,offset: Int,pageSize: Int): MutableMap<String, Any> {
        val map = mutableMapOf<String, Any>()
        if(userId.isNotEmpty()){
            map["userId"] = userId
        }
        map["pageSize"] = pageSize
        map["pageNo"] = offset
        return map
    }

    private suspend fun requestGetCompanyList(
        companyName: String,
        bindingStatus: Int,
        offset: Int,
        pageSize: Int
    ): ResultResponse<CompanyList> {
        val response = companyApi.getCompanyList(
            requestBodyGetCompanyList(
                companyName,
                bindingStatus,
                offset,
                pageSize
            )
        )
        return executeEntityResponse(response, {

        })
    }

    suspend fun getCompany(companyId: String): ResultResponse<Company> {
        return safeApiCallEntity(
            call = { requestGetCompany(companyId) },
            errorMessage = "获取搜索公司信息数据错误"
        )
    }

    private suspend fun requestGetCompany(companyId: String): ResultResponse<Company> {
        val response = companyApi.getCompany(requestBodyGetCompany(companyId))
        return executeEntityResponse(response, {

        })
    }

    suspend fun enterCompanyInfo(options: MutableMap<String, Any>): ResultResponse<Any> {
        return safeApiCallEntity(
            call = { requestEnterCompanyInfo(options) },
            errorMessage = "录入公司信息失败"
        )
    }

    private suspend fun requestEnterCompanyInfo(options: MutableMap<String, Any>): ResultResponse<Any> {
        val response = companyApi.enterCompanyInfo(requestJsonBody(options))
        return executeEntityResponse(response, {

        })
    }

    suspend fun editCompanyInfo(options: MutableMap<String, Any>): ResultResponse<Any> {
        return safeApiCallEntity(
            call = { requestEditCompanyInfo(options) },
            errorMessage = "编辑公司信息失败"
        )
    }

    private suspend fun requestEditCompanyInfo(options: MutableMap<String, Any>): ResultResponse<Any> {
        val response = companyApi.editCompanyInfo(requestJsonBody(options))
        return executeEntityResponse(response, {

        })
    }

    suspend fun companyFromCoChain(companyId: String): ResultResponse<Any> {
        return safeApiCallEntity(
            call = { requestCompanyFromCoChain(companyId) },
            errorMessage = "公司上链错误失败"
        )
    }

    private suspend fun requestCompanyFromCoChain(companyId: String): ResultResponse<Any> {
        val response = companyApi.companyFromCoChain(requestBodyCompanyFromCoChain(companyId))
        return executeEntityResponse(response, {

        })
    }

    suspend fun getUserCoChainList(userId: String,offset: Int,pageSize: Int = 10): ResultResponse<CoChainRecordList>{
        return safeApiCallEntity(
            call = { requestGetUserCoChainList(userId,offset, pageSize) },
            errorMessage = "获取公司上链记录失败"
        )
    }

    private suspend fun requestGetUserCoChainList(userId: String,offset: Int,pageSize: Int): ResultResponse<CoChainRecordList> {
        val response = companyApi.getUserCoChainList(requestBodyGetUserCoChainRecord(userId,offset, pageSize))
        return executeEntityResponse(response, {

        })
    }

}